Sending SMS via Cron Job PHP


Today I will show a trick to send SMS via your Linux server in the background by executing a PHP script file at a specific time using Cron Job. The primary requirement for this tutorial is a Linux server, an SMS package, and an ssh client software like putty.

I am assuming that you know how to config putty and know basic Linux commands for navigation etc.  before you proceed this tutorial. A Cron job is a Linux command for scheduling a task to be executed sometime in the future. This is normally used to schedule a job that is executed periodically - for example, to send out a notice every morning.

You can execute a PHP script file via crontab.

crontab -e

The above command will open a file in nano which contains all your Cron jobs.  To add a cronjob to execute your PHP script file add the following command at end of file.

45 07 * * * /usr/bin/php /var/www/html/sendsms.php

Save and exit nano.  The above command will execute your PHP script file by using PHP installed in usr/bin every day at 7:45 am.

Each line of a crontab file represents a job, and looks like this:

 ┌───────────── minute (0 - 59)
 │ ┌───────────── hour (0 - 23)
 │ │ ┌───────────── day of month (1 - 31)
 │ │ │ ┌───────────── month (1 - 12)
 │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
 │ │ │ │ │                                       7 is also Sunday)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *  command to execute

To view all your  cronjob entries type

crontab -l

Now for sending SMS part. As you guys know you need a Bulk SMS subscription and their PHP API. One of the free SMS providers is way2sms. But they don't provide any official PHP library but still, you can use a library at your own risk by downloading from here. Note you are not legally permitted to use above library but since it is for learning purpose you can try it. 

I use ealerts4you.in SMS gateway. It is paid but fast and cheap. The code for sendsms.php file using ealerts API is given below.

<?php 
 
function httpGet($url)
{
    $ch = curl_init();  
         
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
         
    $output=curl_exec($ch);
         
    curl_close($ch);
    return $output;
}
		
$mobileNumber = ["mobile no 1","mobile no 2"];
$smstxt="Good Morning Have a Nice Day.";

foreach ($mobileNumber as $mobile)
{
	$data = array('uname' => 'sjcet',
				  'password' => 'as provided by ealert',
				  'sender' => 'as provided by ealert',
				  'sms' => $smstxt,
				  'receiver' => $mobile,
				  'msgtype' => '1',
				  'route' => 'TA',
				  'routetype' => '1');
		
	$url="http://ealerts4you.in/httpapi/smsapi?" .http_build_query($data);

	httpGet($url);
}

If you need any help or have any suggestions then please comment below.


Similar Posts

Linux
22nd Jul 2018 02:00:11 AM
PHP CodeIgniter Laravel Linux Ubuntu
8519

ShareurCodes

ShareurCodes is a code sharing site for programmers to learn, share their knowledge with younger generation.